package com.github.baichuan.application.scaffold.jpa.app;

import com.github.baichuan.application.scaffold.domain.app.AppVersion;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import lombok.Builder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

@Builder
public class AppVersionSpecification implements Specification<AppVersion> {
    private String appId;

    @Override
    public Predicate toPredicate(Root<AppVersion> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        Predicate predicate = builder.conjunction();

        if(StringUtils.isNotBlank(appId)) {
            predicate = builder.and(predicate, builder.equal(root.get("app").get("id"), appId));
        }

        return predicate;
    }
}
